<?php

require_once('../domain/Saison.php');


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of KilometerEntryDAO
 *
 * @author riemerb
 */
class SaisonDAO {

    /**
     * Liefert alle Saisons absteigend geordnet.
     * @global type $db
     * @return type
     */
    public static function getSaisons() {
        global $db;
        $query = 'select id as id, DATE_FORMAT(beginn, \'%d.%m.%Y\') AS beginn, DATE_FORMAT(ende, \'%d.%m.%Y\') AS ende from saison where beginn <= CURDATE() order by DATE_FORMAT(ende, \'%Y.%m.%d\') desc';
        try {
            $statement = $db->prepare($query);
            $statement->execute();
            $results = $statement->fetchAll();
            $statement->closeCursor();
            $saisons = SaisonDAO::genDBO($results);
            return $saisons;
        } catch (PDOException $e) {
            $error_message = $e->getMessage();
            display_db_error($error_message);
        }   
    }
    
    public static function genDBO($results) {
        $saisons = array();
        foreach ($results as $row) {
            $saison = new Saison(
                            $row['id'],
                            $row['beginn'],
                            $row['ende']);
            $saisons[] = $saison;
        }
        return $saisons;
    }
}

?>
